layui.define(['layer', 'form', 'dropdown', 'laydate', 'laytpl', 'table', 'glfiles', 'lvybtpl'], function (exports) {
    var MOD_NAME = 'contract',
        form = layui.form,
        dropdown = layui.dropdown,
        layer = layui.layer,
        laytpl = layui.laytpl,
        table = layui.table,
        glfiles = layui.glfiles,
        lvybtpl = layui.lvybtpl,
        kaige = layui.kaige,
        laydate = layui.laydate;
    contract = function () {
    };
    contract.prototype.addReceivables = function () {
        console.log("添加回款开始");
        window.pagedict = parent.window.kaige_page.dict || {};
        var contract_id = $("#contract_id").val();
        var customer_id = $("#customer_id").val();
        var contract_money = $("#contract_money").val();
        lvybtpl.ajax({
            jq: jq,
            type: 'get',
            url: szoa.client.path + '/pages/crm/contract/receivables.html',
            id: 'receivables',
            detailData: {
                pagedict: window.pagedict,
                contract_id: contract_id,
                customer_id: customer_id,
            },
            postUrl: '/crm.receivables/add',
            callafter: function () {
                laydate.render({
                    elem: '#return_time'
                    , type: 'date'
                });
            },
            contentTable: 'receivablesTable',
        });
        console.log("添加回款结束");
    }
    contract.prototype.addReceivablesPlan = function () {
        console.log("添加回款计划开始");
        window.pagedict = parent.window.kaige_page.dict || {};
        var contract_id = $("#contract_id").val();
        var customer_id = $("#customer_id").val();
        var contract_money = $("#contract_money").val();
        lvybtpl.ajax({
            jq: jq,
            type: 'get',
            url: szoa.client.path + '/pages/crm/contract/receivables_plan.html',
            id: 'receplan',
            detailData: {
                pagedict: window.pagedict,
                contract_id: contract_id,
                money: contract_money,
                customer_id: customer_id,
            },
            postUrl: '/crm.receivables_plan/add',
            callafter: function () {
                laydate.render({
                    elem: '#return_date'
                    , type: 'date'
                });
            },
            contentTable: 'receivablesPlanTable',
        });
        console.log("添加回款计划结束");
    }

    contract.prototype.getReceivables = function () {
        var contract_id = $("#contract_id").val();
        var customer_id = $("#customer_id").val();
        table.render({
            elem: '#receivablesTable',
            url: "/crm.receivables/index",
            id: 'receivablesTable',
            cols: [[
                {field: 'number', title: '回款编号', align: 'center'},
                {field: 'money', title: '金额', align: 'center'},
                {field: 'return_time', title: '回款日期', align: 'center'},
                {field: 'return_type_id', title: '回款方式', align: 'center', templet: '#return_type_id_name'},
                {field: 'remarks', title: '备注', align: 'center'},
                {field: 'owner_user_name', title: '负责人姓名', align: 'center'},
            ]],
            where: {
                contract_id: contract_id,
                customer_id: customer_id,
            }
        });
        table.on('rowDouble(receivablesTable)', function (obj) {
            var data = obj.data;
            //editRecord(data.id);
        });
    }

    contract.prototype.getReceivablesPlan = function () {
        var contract_id = $('#contract_id').val();
        table.render({
            elem: '#receivablesPlanTable',
            url: "/crm.receivables_plan/index",
            id: 'receivablesPlanTable',
            cols: [[
                {field: 'num', title: '期数', align: 'center'},
                {field: 'money', title: '计划回款金额', align: 'center'},
                {field: 'return_date', title: '计划回款日期', align: 'center'},
                {field: 'return_type_id', title: '计划回款方式', align: 'center', templet: '#return_type_id_name'},
                {field: 'remind', title: '提前几天提醒', align: 'center'},
                {field: 'remarks', title: '备注', align: 'center'},
            ]],
            where: {
                contract_id: contract_id,
            }
        });
        table.on('rowDouble(contentTable)', function (obj) {
            var data = obj.data;
            //editRecord(data.id);
        });
    }
    contract.prototype.addProductEvent = function () {
        var contract_product_ids = $("#contract_product_ids").val();

        jq.ajax({
            type: 'get',
            url: szoa.client.path + '/pages/crm/contract/product.html',
            data: {time: Date.now(), contract_product_ids: contract_product_ids},
            dataType: 'html',
            success: function (res) {
                var index2 = layer.open({
                    id: 'form_form_box',
                    type: 1,
                    anim: 4,
                    offset: 'r',
                    area: ['650px', '100%'],
                    title: "产品页面",
                    content: res,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        $('#product_form_btn_save').click();
                    },
                    success: function () {
                        window.showFormPage();
                        //提交产品数据
                        form.on('submit(product_form_lay_filter)', function (data) {
                            var d2 = {};
                            var _data = $("#product_form_lay").serialize();
                            var d = _data.split("&");
                            $.each(d, function (index, item) {
                                te = [];
                                te = item.split("=")
                                d2[te[0]] = te[1];
                            });
                            $.ajax({
                                type: 'POST',
                                url: "/crm.contract/saveProduct",
                                data: d2,
                                success: function (res2) {
                                    if (res2.status) {
                                        var id = [];
                                        var money = 0;
                                        $.each(res2.data, function (index, item) {
                                            id.push(item.id);
                                            money = item.price * item.num;
                                        });
                                        var ids = id.join(",");
                                        $("#contract_product_ids").val(ids);
                                        $("#money").val(money);
                                        layer.msg(res2.msg, {time: 1300}, function () {
                                            layer.close(index2);
                                        });
                                    } else {
                                        layer.msg(res2.msg);
                                    }
                                }
                            });
                            return false;
                        });

                    }, end: function () {
                    }, cancel: function (index, layero) {
                        layer.close(index2)
                    }
                });
            }
        });

    }
    //回款信息开始
    contract.prototype.receivablesEvent = function (e) {
        lvybtpl.ajax({
            jq: jq,
            type: 'get',
            url: szoa.client.path + '/pages/crm/contract/receivables.html',
            id: 'receivables',
            detailData: {
                pagedict: window.pagedict,
                customer_id: e.customer_id,
                customer_id_name: e.customer_id_name,
                contract_id: e.id,
                contract_id_name: e.name,
            },
            callafter: function () {
                var select_2 = lvybtpl.xmSelect({
                    url: '/select/Customer'
                    , id: 'customer_id'
                    , getData: {automatch: 1}
                    , values: [e.customer_id]
                    , callafter: function () {
                        var customer_id = $("#customer_id").val();
                        lvybtpl.xmSelect({
                            url: '/select/Contract'
                            , id: 'contract_id'
                            , values: [e.id]
                            , getData: {automatch: 1, customer_id: customer_id}
                        });
                    }
                });
                laydate.render({
                    elem: '#return_time'
                    , type: 'date'
                });
            },
            postUrl: '/crm.receivables/add',
            contentTable: 'pageTable',
        });
    }
    //回款信息结束


    contract.prototype.getRecord = function () {
        var contract_id = $('#contract_id').val();
        kaige.table({
            elem: '#recordTable',
            url: "/crm.contract_record/index",
            id: 'recordTable',
            cols: [[
                {field: 'follow_type_name', title: '跟进类型', align: 'center'},
                {field: 'follow_time', title: '跟进时间', align: 'center'},
                {field: 'follow_name', title: '跟进状态', align: 'center'},
                {field: 'content', title: '跟进内容', align: 'center'},
                {field: 'next_time', title: '下次跟进时间', align: 'center'},
            ]],
            where: {
                relation_id: contract_id,
            }
        });
        table.on('rowDouble(contentTable)', function (obj) {
            var data = obj.data;
            //editRecord(data.id);
        });
    }
    contract.prototype.addRecordEvent = function () {
        console.log("添加跟进开始");
        window.pagedict = parent.window.kaige_page.dict || {};
        var contract_id = $("#contract_id").val();
        var contract_id_name = $("#contract_id_name").val();
        lvybtpl.ajax({
            jq: jq,
            type: 'get',
            url: szoa.client.path + '/pages/crm/contract/record.html',
            id: 'record',
            detailData: {
                pagedict: window.pagedict,
                relation_id: contract_id,
                relation_id_name: contract_id_name,
            },
            postUrl: '/crm.contract_record/add',
            callafter: function () {
                lvybtpl.xmSelect({
                    url: '/select/Contract'
                    , id: 'relation_id'
                    , getData: {automatch: 1}
                    , values: [contract_id]
                });
                laydate.render({
                    elem: '#next_time'
                    , type: 'date'
                });

                $('#reminds_name').on('focus', function () { //rece_id_name 要调用的文本框
                    layer.open({
                        title: '选择适用对象',
                        content: szoa.client.path + '/pages/pmm/config/ugdc.html',
                        area: ['690px', '470px'],
                        type: 2,
                        success: function (layero, index) {
                            window.ugdcinfo = function (opt) {
                                $('#reminds_name').val(opt.names);
                                $('#reminds_id').val(opt.ids); // 隐藏的文本框存 id ,部门id(u1,u4),组id(g1,g2),部门根ID（c1,c9)与部门ID区别 根ID需要查找同所有子部门
                                layer.close(layer.index);
                            }
                        }
                    });
                });
            },
            contentTable: 'recordTable',
        });
        console.log("添加跟进结束");
    }


    var contract = new contract();
    exports(MOD_NAME, contract)
})